package top.yamhk.agent.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import top.yamhk.agent.config.WechatApp;
import top.yamhk.agent.utils.BeanUtils8Spring;

/**
 * #
 *
 * @date 2022/02/22 22:22
 */
@Slf4j
@EnableAsync
//@EnableCaching
@EnableScheduling
@ComponentScans({@ComponentScan(value = "top.yamhk.agent.config")
        , @ComponentScan({"top.yamhk.plugin", "top.yamhk.agent"})
        , @ComponentScan(value = "top.yamhk.plugin.feign")
        , @ComponentScan(value = "top.yamhk.runner")
})
@EnableConfigurationProperties({WechatApp.class})
@Import({BeanUtils8Spring.class})
@Order(999)
public class SpringBootBeanInit implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        this.applicationContext = ctx;
    }

    @Override
    public void afterPropertiesSet() {
        log.warn("SpringBootBeanInit");
        // TODO: 2023/8/24 yxy 创建go脚本
        //钩子
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                System.err.println("是你吗小钩子!");
                System.err.println("是你吗小钩子!");
                System.err.println("是你吗小钩子!");
            } catch (Exception e) {
                log.error("-{}", e.getMessage());
            }
        }));
    }
}